package com.bitcola.security.me.rest;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.bitcola.security.auth.client.i18n.ColaLanguage;
import com.bitcola.security.common.msg.AppResponse;
import com.bitcola.security.me.biz.ColaAppBannerBiz;
import com.bitcola.security.me.biz.ColaCoinSymbolBiz;
import com.bitcola.security.me.feign.IDataServiceFeign;
import com.bitcola.security.me.mapper.NewsMapper;
import com.bitcola.entity.ColaAppBanner;
import com.bitcola.me.entity.News;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  @author wx
 * @create 2019-03-20 11:04
 **/
@RestController
@RequestMapping("app")
public class AppController {

    @Autowired
    ColaAppBannerBiz bannerBiz;


    @Autowired
    ColaCurrencyController colaCurrencyController;

    @Resource
    IDataServiceFeign dataServiceFeign;

    @Autowired
    ColaCoinSymbolBiz colaCoinSymbolBiz;
    @Autowired
    NewsMapper newsMapper;

    @RequestMapping("banner")
    public AppResponse banner(){
        List<ColaAppBanner> list = bannerBiz.banner(ColaLanguage.getCurrentLanguage());
        return AppResponse.ok().data(list);
    }

    @RequestMapping("config")

    public AppResponse config(){
        Map<String,Object> map=new HashMap<>(6);
        map.put("androidVersion",dataServiceFeign.getConfig("android_version"));
        map.put("androidVersionV2",dataServiceFeign.getConfig("android_version_v2"));

        map.put("iosVersion",dataServiceFeign.getConfig("ios_version"));
        Object rate=colaCurrencyController.getExchangeRate().getData();
        map.put("rate",rate);
        Object banner= bannerBiz.banner(ColaLanguage.getCurrentLanguage());
        map.put("banner",banner);

        Object coins=colaCoinSymbolBiz.getCoinSymbolBySymbol(null);
        map.put("coins",coins);
        map.put("androidUrl",dataServiceFeign.getConfig("android_url"));
        map.put("iosUrl",dataServiceFeign.getConfig("ios_url"));
        map.put("new_future",dataServiceFeign.getConfig("new_future"));
        map.put("share_url",dataServiceFeign.getConfig("share_url"));

        return AppResponse.ok().data(map);



    }

    @RequestMapping("lastNews")
    @Cached(cacheType = CacheType.LOCAL,expire = 30)

    public AppResponse lastNews(int page ,int size)
    {

        Example example=new Example(News.class);
        example.orderBy("time").desc();
        return AppResponse.ok().data(newsMapper.selectByExampleAndRowBounds(example, new  RowBounds((page-1)*size,size)));


    }
    @RequestMapping("newsDetails")
    @Cached(cacheType = CacheType.LOCAL,expire = 30)

    public AppResponse lastNews(String  id )
    {


        return AppResponse.ok().data(        newsMapper.selectByPrimaryKey(id).getContent());



    }

}
